import wx

class Import(wx.Panel):
    """
    This will be the first notebook tab
    """
    #----------------------------------------------------------------------
    def __init__(self, parent, model):
        """"""

        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.model = model
        self.list = wx.ListBox(self, 26, wx.DefaultPosition, (600, 300), [], wx.LB_SINGLE)
        add = wx.Button(self, 1, 'Add')
        remove = wx.Button(self, 2, 'Remove')
        wx.EVT_BUTTON(self, 1, self.OnAddFile)
        wx.EVT_BUTTON(self, 2, self.OnRemove)
        #self.list.SetSelection(0)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.location = wx.TextCtrl(parent=self, id=wx.ID_ANY, size=(500, 20), value="")
        browse = wx.Button(self, 3, 'Browse')
        wx.EVT_BUTTON(self, 3, self.OnBrowse)

        buttons = wx.BoxSizer(wx.HORIZONTAL)
        buttons.Add(add, 0, wx.ALL, 5)
        buttons.Add(remove, 0, wx.ALL, 5)

        locationSizer = wx.BoxSizer(wx.HORIZONTAL)
        locationSizer.Add(self.location, 0, wx.ALL, 5)
        locationSizer.Add(browse, 0, wx.ALL, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list, 0, wx.ALL, 5)
        sizer.Add(locationSizer, 0, wx.ALL, 5)
        sizer.Add(buttons)

        self.SetSizer(sizer)

    def OnBrowse(self, event):
        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultFile="",
            style=wx.OPEN | wx.CHANGE_DIR
        )
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.location.SetValue(path)

    def OnAddFile(self, event):
        location = self.location.GetValue()
        self.list.Insert(self.model.addFile(location), 0)
        self.location.SetValue("")

    def OnRemove(self, event):
        selections = list(self.list.GetSelections())
        self.list.Delete(selections[0])
        self.model.removeFile(self.list.GetStrings()[selections[0]])

    def getFiles(self):
        return self.list

    
    def update(self):
        self.list.Clear()
        print self.model.getFiles()
        for item in self.model.getFiles():
            self.list.Insert(item, 0)
